package com.mooral.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.mooral.context.Context;
import com.mooral.context.ContextSession;
import com.mooral.web.tracker.*;


public abstract class MooralAction extends Action {

    protected Context context;
    protected HttpServletRequest request;
    protected static Log log = LogFactory.getLog(MooralAction.class);
    protected String title = "VOUCHEAP";
    protected String description = "VOUCHEAP";
    protected String keywords = "VOUCHEAP";
    
    /**
     * Define la cadena que se utilizar� para hacer referencia a los "forwards"
     * de �XITO dentro de cada acci�n.
     */
    public static final String ACTION_SUCCESS = "success";

    /**
     * Define la cadena que se utilizar� para hacer referencia a los "forwards"
     * de FRACASO dentro de cada acci�n.
     */
    public static final String ACTION_FAILURE = "failure";

    public final ActionForward execute(ActionMapping mapping, ActionForm form,
	    HttpServletRequest request, HttpServletResponse response) throws Exception {

		this.request = request;
		context = ContextSession.getCurrentContext();
		
		Long timeInit = System.currentTimeMillis();
		log.debug("[" + context.getSessionId() + "] - " + "Iniciando peticion de accion Java (action-struts). Clase: "
			+ this.getClass().getSimpleName());
	
		ActionForward af;
		try {
		    af = perform(mapping, form, request, response, context);
		    
		    /* MR
		    request.setAttribute("headerTitle", title);
		    request.setAttribute("metaDescription", description);
		    request.setAttribute("metaKeywords", keywords);
		    */
		    
		    Tracker.SaveAction(request.getHeader("Referer"), request.getRequestURI(), form, request.getRemoteHost());
		} catch (Throwable t) {
		    log.error("[" + context.getSessionId() + "] - " + "Error - Mensaje : " + t.getMessage());
			throw new Exception(t.getMessage());
	    }
	
		log.debug("[" + context.getSessionId() + "] - " + "Finalizando peticion de accion Java (action-struts). Clase: "
				+ this.getClass().getSimpleName() + ". Tiempo de ejecucion: "
				+ (System.currentTimeMillis() - timeInit));
		
		return af;
    }

    public abstract ActionForward perform(ActionMapping mapping, ActionForm form,
	    HttpServletRequest request, HttpServletResponse response, Context context)
	    throws Exception;


}
